
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Setup(offset)
	if self:GetOwner() == nil then return end
	self.Offset = offset	
end

function ENT:GetOffset()
return self.Offset end

function ENT:SetPlayer(v)
	self:SetOwner(v)
	self.ReloadingTimer = CurTime()
	self.ShotTimer = CurTime()
end

function ENT:SetUser(user)
	self.User = user
end

function ENT:GetMuzzlePos()
	local vec
	vec = self:GetTrueUp() * 2
return vec end

function ENT:GetTrueUp()
	return self:GetUp()
end

function ENT:GetTrueForward()
	return self:GetForward()
end

function ENT:GetTrueRight()
	return self:GetRight()
end

function ENT:Initialize()

	// Change this per weapon
	self.Model = "models/weapons/w_mach_m249para.mdl"
	self.ReloadTime = 5
	self.Clip = 200
	self.Rof = .3
	self.Damage = 2
	self.BulletForce = 10
	self.Cone = 0
	self.NumBullets = 1
	self.MuzzlePos = Vector(0,0,10) //multiply up, forward, right by p,y,r
	self.Effect = "muzzle_normal"
	self.Sound = Sound("weapons/smg1/smg1_fire.wav")
	self.ReloadSound = Sound("weapons/m249/m249_boxout.wav")
	
	util.PrecacheModel(self.Model)
	
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	
	//this stuff is base only
	self.Reload = false
	self.ReloadingTimer = CurTime()
	self.ShotTimer = CurTime()
	self.CurClip = self.Clip
	
	//set maximum ammo
	self:GetOwner():SetNetworkedFloat("maxvehicleammo",self.Clip)
	self:GetOwner():SetNetworkedFloat("vehicleammo",self.CurClip)
	
end

// p,y,r corresponds with up, forward, right
function ENT:GetWepAngles()
	//local ang = self:GetAngles()
	local ang = self:LocalToWorldAngles(self.Offset)
return ang end

function ENT:TakeAmmo(num)
	self.CurClip = self.CurClip - num
	if self.CurClip <= 0 then
		self:ReloadWeapon()
		self:GetOwner():SetNetworkedFloat("vehicleammo",self.CurClip)
		self.CurClip = self.Clip
		return
	end
	self:GetOwner():SetNetworkedFloat("vehicleammo",self.CurClip)
end

function ENT:ReloadWeapon()
	self:EmitSound( Sound("weapons/p90/p90_boltpull.wav"),100,math.random(90,110))
	self:EmitSound( self.ReloadSound, 100, math.random(95,105) )
	self.Reload = true
	self.ReloadingTimer = CurTime() + self.ReloadTime
	self:GetOwner():SetNetworkedFloat( "reloadtime", self.ReloadingTimer )
	self:GetOwner():SetNetworkedFloat( "basetime", self.ReloadTime )
end

function ENT:IsReloading()
	if self.ReloadingTimer > CurTime() then
		return true
	else
		self.Reload = false
		return false
	end
end

function ENT:SetNextFire()
	self.ShotTimer = self.Rof + CurTime()
end

function ENT:CanFire()
return self.ShotTimer < CurTime() and !self.Reload end

function ENT:FireBullet( damage, force, num_bullets, aimcone, effect, firesnd, pitch, tracer, hiteffect)

	if self.User == nil then return end
	
	local bullet = {}
	bullet.Num 		= num_bullets
	bullet.Src 		= self:GetMuzzlePos()
	bullet.Dir 		= self:GetOwner():GetAimVector()//self:GetTrueForward()//self:GetWepAngles()			
	bullet.Spread 	= Vector( aimcone, aimcone, 0 )		
	bullet.Tracer	= 1				
	bullet.Force	= force					
	bullet.Damage	= damage
	bullet.AmmoType = "Pistol"
	bullet.TracerName 	= tracer
	
	local mats = {MAT_CONCRETE,
	MAT_DIRT,
	MAT_WOOD,
	MAT_TILE,
	MAT_SLOSH,
	MAT_SAND,
	MAT_FOLIAGE,
	MAT_PLASTIC}
	
	if hiteffect then
		bullet.Callback = function ( attacker, tr, dmginfo ) 
			if table.HasValue(mats,tr.MatType) then
				local effectdata = EffectData()
				effectdata:SetOrigin(tr.HitPos)
				effectdata:SetAngle(tr.HitNormal)
				util.Effect(hiteffect, effectdata)
			end
		end
	end

	self:EmitSound(firesnd,100,math.random(pitch[1],pitch[2]))
	
	self:GetOwner():FireBullets( bullet )
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetMuzzlePos())
	effectdata:SetStart(self:GetVelocity())
	effectdata:SetAngle(self:GetTrueForward():Angle())
	util.Effect(effect, effectdata)

	self:TakeAmmo(num_bullets)
	self:SetNextFire()
	
end

function ENT:LockOn()
	local tracedata = {}
	tracedata.start = self:GetMuzzlePos()
	tracedata.endpos = self:GetWepAngles():Forward() * 99999
	tracedata.filter = self
	local trace = util.TraceLine(tracedata)
	if not trace.Entity then return nil end
	if trace.Entity:IsPlayer() then return trace.Entity end
	if string.find(trace.Entity:GetClass(), "vehicle_") or string.find(trace.Entity:GetClass(), "beacon") then
		return trace.Entity
	end
end

function ENT:FireRocket( entityname, effect, sound1 )
	
	if self.User == nil then return end
	
	local ent = ents.Create(entityname)
	ent:SetPos(self:GetMuzzlePos())
	ent:SetPlayer(self:GetOwner())
	ent:SetAngles(self:GetWepAngles())
	ent:Activate()
	ent:Spawn()
	
	self:EmitSound( sound1, 100, math.random(95,105) )
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetMuzzlePos())
	effectdata:SetAngle(self:GetWepAngles())
	util.Effect(effect, effectdata)
	
	self:TakeAmmo(1)
	self:SetNextFire()
	
end

function ENT:DropBomb( entityname, delay, snd1, pitch ,snd2) //snd2 optional

	if self.User == nil then return end
	
	local ent = ents.Create(entityname)
	ent:SetPos(self:GetPos())
	ent:SetPlayer(self:GetOwner())
	ent:SetAngles(self:GetOwner():EyeAngles())
	ent:Activate()
	ent:Spawn()
	
	if snd2 then
		self:EmitSound(snd2,100,math.random(pitch[1],pitch[2]))
	end
	
	constraint.NoCollide( ent, self, 0, 0 ); 
	ent:SetParent(self)

	ent:SetLauncher(self)
	ent:DelayedParent(delay)
	ent:DelayedSound(snd1,delay,math.random(pitch[1],pitch[2]))
	
	self:TakeAmmo(1)
	self:SetNextFire()
	
end

function ENT:PhysicsSimulate(phys, frametime)
	phys:Wake()
end

function ENT:Think()
	self:SetNextThink(CurTime())
end

function ENT:OnTakeDamage(dmg)

end 